package com.soboot.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.soboot.base.converter.BeanMapping;
import com.soboot.base.enums.DeleteStatus;
import com.soboot.base.util.StringUtil;
import com.soboot.system.entity.SysDict;
import com.soboot.system.mapper.SysDictMapper;
import com.soboot.system.model.QueryDictListReqModel;
import com.soboot.system.service.ISysDictService;
import com.soboot.system.vo.DictVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author:
 * @createTime: 2023年01月05日 14:56:40
 * @version:
 * @Description:
 * @Copyright:
 */
@Service
public class DictServiceImpl extends ServiceImpl<SysDictMapper, SysDict> implements ISysDictService {

    @Autowired
    private SysDictMapper sysDictMapper;

    @Override
    public IPage<SysDict> page(QueryDictListReqModel reqModel) {

        QueryWrapper<SysDict> wrapper = new QueryWrapper<>();
        wrapper.eq("deleted",DeleteStatus.NOT_DELETE.getValue());
        wrapper.like(StringUtil.isNotBlank(reqModel.getDictName()), "dict_name", reqModel.getDictName());
        wrapper.like(StringUtil.isNotBlank(reqModel.getDictCode()), "dict_code", reqModel.getDictCode());
        IPage<SysDict> page = sysDictMapper.selectPage(new Page<>(reqModel.getPageNum(), reqModel.getPageSize()), wrapper);
        return page;
    }

    @Override
    public List<DictVO> getList() {
        return null;
    }

    @Override
    public DictVO detail(Integer id) {

        SysDict sysDict = sysDictMapper.selectById(id);
        return BeanMapping.map(sysDict, DictVO.class);
    }

    @Override
    public void delete(Integer id) {

        UpdateWrapper<SysDict> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",id).set("deleted", DeleteStatus.DELETE.getValue());
        sysDictMapper.update(null,wrapper);
    }
}
